package Bluetooth.find;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.widget.TextView;

public class BTClientThread extends Thread {
	private final BluetoothSocket ClientSocket;
    private final BluetoothDevice device;
    private InputStream inputStream=null;
    private OutputStream outputStream=null;
    TextView tv;
    public String msg;
    public static BluetoothAdapter adap;
    private Handler handler= new Handler();
    static UUID UUID_RFCOMM_GENERIC = new UUID(0x0000110100001000L,0x800000805F9B34FBL);
    public BTClientThread(BluetoothDevice device,BluetoothAdapter adap,String msg,TextView tv)
    {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
         this.device = device;
        this.adap=adap;
        this.msg=msg;
        this.tv=tv;
 
        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try{
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
    		
      /*  Method method;
        try {
        	    method = device.getClass()
        	        .getMethod("createRfcommSocket", new Class[] { int.class});
        	    tmp = (BluetoothSocket) method.invoke(device,1);
        	
        } catch (Exception e) {}*/
    	}catch(Exception e1)
    	{
    		
    	}
        
        ClientSocket = tmp;
        
      
    }
    public void post(final String s)
    {
            handler.post(new Runnable() {
        public void run() {
            tv.setText("\n"+s);
        }
    });
    }
    public void run() {
        // Cancel discovery because it will slow down the connection
    	//tv.setText("in client");
        adap.cancelDiscovery();
       int size=msg.length();
        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
        	//tv.setText("trying to connect");
            ClientSocket.connect();
          //  tv.setText(" connected");
        } catch (IOException connectException) {
        	//tv.setText("could not connect");
        }
            // Unable to connect; close the socket and get out
        	InputStream tmpIn = null;
            OutputStream tmpOut = null;
     
            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = ClientSocket.getInputStream();
                tmpOut = ClientSocket.getOutputStream();
            } catch (IOException e) { }
     
            inputStream = tmpIn;
            outputStream = tmpOut;
            byte[] writebytes = msg.getBytes();
        	try {
                outputStream.write(writebytes);
            } catch (IOException e) { }
        	byte[] buffer = new byte[25];  // buffer store for the stream
            int bytes; // bytes returned from read()
            
        	//while (true) {
                try {
                    // Read from the InputStream
                    bytes = inputStream.read(buffer);
                    
                    // Send the obtained bytes to the UI activity
               //   tv.setText(new String(buffer));
                    String str= new String(buffer);
                   // BluetoothActivity.printing(str);
              //      tv.setText(str);
                    String str1= str.substring(0, buffer.length);
                    post(str1);
                } catch (IOException e) {}
                   
      }
           // }
        	/*try {
                ClientSocket.close();
            } catch (IOException closeException) { }
            return;
        }
        
 
        // Do work to manage the connection (in a separate thread)
        
    
 
    /** Will cancel an in-progress connection, and close the socket */
  /*  public void cancel() {
        try {
            ClientSocket.close();
        } catch (IOException e) { }
    }*/
}
